/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.math.data;

import org.math.data.storage.Storage;

/**
 * This class encapsulates a scalar. We have used double storage for everything. But for high precision computing
 * we may extend this cla
 * @author stewari1
 */
public class ScalarDataValue extends DataValue {

    /** Field description */
    private Storage storage = null;

    /**
     * Constructs ...
     *
     * @param storage description
     */
    public ScalarDataValue(Storage storage) {

        super(Dimension.SCALAR);

        this.storage = storage;
    }

    Storage getStorage() {
        return storage;
    }

    void setStorage(Storage storage) {
        this.storage = storage;
    }

    /**
     * @throws ClassCastException
     */
    @Override
    public DataValue plus(DataValue dataValue) {

        ScalarDataValue scalarDataValue = (ScalarDataValue) dataValue;

        return new ScalarDataValue(this.getStorage().plus(scalarDataValue.getStorage()));
    }

    /**
     * @throws ClassCastException
     */
    @Override
    public DataValue minus(DataValue dataValue) {

        ScalarDataValue scalarDataValue = (ScalarDataValue) dataValue;

        return new ScalarDataValue(this.getStorage().minus(scalarDataValue.getStorage()));
    }

    /**
     * @throws ClassCastException
     */
    @Override
    public DataValue times(DataValue dataValue) {

        ScalarDataValue scalarDataValue = (ScalarDataValue) dataValue;

        return new ScalarDataValue(this.getStorage().times(scalarDataValue.getStorage()));
    }

    /**
     * @throws ClassCastException
     */
    @Override
    public DataValue inverse() {
        return new ScalarDataValue(this.getStorage().inverse());
    }

    /**
     * @throws ClassCastException
     */
    @Override
    public DataValue divide(DataValue dataValue) {

        ScalarDataValue scalarDataValue = (ScalarDataValue) dataValue;

        return new ScalarDataValue(this.getStorage().divide(scalarDataValue.getStorage()));
    }

    @Override
    public void changeToDoubleStorage() {
        setStorage(storage.doubleStorage());
    }

    @Override
    public void changeToFloatStorage() {
        setStorage(storage.floatStorage());
    }

    @Override
    public void changeToIntStorage() {
        setStorage(storage.intStorage());
    }

    @Override
    public void changeToLongStorage() {
        setStorage(storage.longStorage());
    }

    @Override
    public void changeToShortStorage() {
        setStorage(storage.shortStorage());
    }

    @Override
    public boolean isLessThan(double epsilon) {
        return this.storage.isLessThan(epsilon);
    }

    @Override
    public boolean isGreaterThan(double epsilon) {
        return this.storage.isGreaterThan(epsilon);
    }

    @Override
    public boolean isEqualTo(double epsilon) {
        return this.storage.isEqualTo(epsilon);
    }

    @Override
    public DataValue copy() {
        return new ScalarDataValue(this.storage.copy());
    }

    @Override
    public DataValue pow(int n) {

        double value       = this.storage.doubleValue();
        Storage newStorage = this.storage.copy();

        newStorage.setValue(Math.pow(value, n));

        return new ScalarDataValue(newStorage);
    }

    @Override
    public double getValue() {
        return this.storage.doubleValue();
    }

    @Override
    public void setValue(double value) {
        this.storage.setValue(value);
    }

    @Override
    public DataValue abs() {
        return new ScalarDataValue(this.storage.abs());
    }
}
